package com.qd.videorecorder;

import java.io.File;
import java.io.FileFilter;

import com.qd.videorecorder.adapter.LocalMp3Adapter;
import com.qd.videorecorder.util.RecordMixMp3;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.ViewInject;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class SingLocalMusic extends FinalActivity {
	
	static {
		System.loadLibrary("mad");  
	}  
	
	@ViewInject(id=R.id.main_btn_below , click="viewClick") Button main_btn_below;
	@ViewInject(id=R.id.topbar_title ) TextView topbar_title;
	@ViewInject(id=R.id.btn_myring , click="viewClick") Button btn_myring;
	@ViewInject(id=R.id.lv_local_mp3) ListView lv_local_mp3;
	@ViewInject(id=R.id.ll_alert) LinearLayout ll_alert;
	@ViewInject(id=R.id.tv_alert) TextView tv_alert;
	
	private LocalMp3Adapter localMp3Adapter;
	
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.singandmusic);
		
		main_btn_below.setBackgroundResource(R.drawable.icon_return);
		btn_myring.setText("在线背景音乐");
		
		String title = getIntent().getStringExtra("title");
		if(title==null){
			title = getResources().getString(R.string.app_name);
		}
		topbar_title.setText(title);
		
		localMp3Adapter = new LocalMp3Adapter(this);
		
		initMusic();
	}
	
	private void initMusic(){
		
		String pcmPath = RecordMixMp3.getLocalPath() + "/music/pcm/";
		String recordPath = RecordMixMp3.getLocalPath() + "/music/record/";
		File pcDir = new File(pcmPath);
		if(!pcDir.exists()){
			pcDir.mkdirs();
		}
		
		File recordDir = new File(recordPath);
		if(!recordDir.exists()){
			recordDir.mkdirs();
		}
		
		String path = RecordMixMp3.getLocalPath() + "/music/";
		String alert = "请将44.1kHz采样率背景音乐放置在\n（暂不支持其他采样率背景音乐）\n"+path+"目录下";
		File file = new File(path);
		if(!file.exists()){
			file.mkdirs();
			lv_local_mp3.setVisibility(View.GONE);
			ll_alert.setVisibility(View.VISIBLE);
			tv_alert.setText(alert);
		}else{
			ll_alert.setVisibility(View.GONE);
			
			FileFilter fileFilter = new FileFilter() {
				
				@Override
				public boolean accept(File pathname) {
					// TODO Auto-generated method stub
					if(pathname.getName().endsWith(".mp3")){
						return true;
					}
					return false;
				}
			};
			
			File[] files = file.listFiles(fileFilter);
			if(files.length>0){
				lv_local_mp3.setVisibility(View.VISIBLE);
				localMp3Adapter.setData(files);
				if(lv_local_mp3.getAdapter()==null){
					System.out.println("lv_local_mp3.getAdapter()==null");
					lv_local_mp3.setAdapter(localMp3Adapter);
				}
				localMp3Adapter.notifyDataSetChanged();
			}else{
				lv_local_mp3.setVisibility(View.GONE);
				ll_alert.setVisibility(View.VISIBLE);
				tv_alert.setText(alert);
			}
			
		}
	}
	
	public native int decodeMp3ToPCM2(String mp3File, String mp3PCM);
	ProgressDialog progressDialog;
	
	private Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what)
				{
				case 0:
					if(progressDialog!=null){
						progressDialog.dismiss();
					}
					break;
				case 1:
					progressDialog = new ProgressDialog(SingLocalMusic.this);
					progressDialog.setMessage("初始化伴奏\n建议佩戴耳机录音...");
					progressDialog.setCancelable(false);
					progressDialog.show();
					break;
				default:
					break;
				}
			
		}
		
	};
	
	public void decode(){
		handler.sendEmptyMessage(1);
	}
	
	public void decodeFinish() {
		// TODO Auto-generated method stub
		handler.sendEmptyMessage(0);
	}
	
	public void viewClick(View v){
		switch (v.getId())
			{
			case R.id.main_btn_below:
				finish();
				break;
			case R.id.btn_myring:
				Intent intent = new Intent();
				intent.setClass(this, RecordLocalMusic.class);
				intent.putExtra("title", "本地录音");
				startActivityForResult(intent, 0);
				break;
			default:
				break;
			}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		
		if(resultCode==RESULT_OK){
			if(requestCode==0){
				if(data.getBooleanExtra("refreash", false))
					initMusic();
			}
		}
		
	}
	
}
